package zoo.cart.admin.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import zoo.cart.admin.dao.CartDao;
import zoo.cart.admin.domain.AdminProductOrderCommand;
import zoo.cart.admin.domain.CartCommand;

@Controller
public class CartCheckController {
	
	@Autowired
	private CartDao cartDao;
		
	@RequestMapping(value="/admin/cartCheck.do",method=RequestMethod.POST)
	public String process1(){
		return "list.do";
	}
	
	@ModelAttribute("cartCommand")
	public CartCommand forback(){
		return new CartCommand();
	}
	
	@RequestMapping(value="/admin/cartCheck.do",method=RequestMethod.GET)
	public ModelAndView process(@RequestParam("or_num")int or_num,
							@RequestParam("or_qtity")int or_qtity,
							@RequestParam("p_code")int p_code){
		
		String formview = "redirect:/admin/cartDetail.do?or_num="+or_num;
		
		int qtity = cartDao.checkQtity(p_code);
		
		if(qtity <= 0){
			return new ModelAndView(formview,"qt",qtity);
		}
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("p_code", p_code);
		map.put("or_qtity",or_qtity);
		
		cartDao.checkCart(or_num);
		cartDao.changePro(map);
		cartDao.updateBuyNum(p_code);
		
		if(qtity == 0){
			cartDao.updateProQcheck(p_code);
		}
		AdminProductOrderCommand member = null;
		member = cartDao.selectProOrderBoard(p_code);
			
		System.out.println("or_num = " +or_num+ "  p_code = " +p_code+ " member = "+member);
		return new ModelAndView("admin_order_result","member",member);
	}
	
	
}
